<?php


namespace App\Http\Controllers\App;


use App\Http\Controllers\Controller;
use App\Models\MemberOrder;
use Auth;
use Carbon\Carbon;

class MemberConsumeController extends Controller
{
    public function data()
    {
        $memberOrder = new MemberOrder();
        $total = $memberOrder->where('is_pay', 1)->sum('money');
        $members = $memberOrder->where('is_pay', 1)->distinct()->count('member_id');
        $self = 0;
        if (Auth::check()) {
            $self = $memberOrder->where('member_id', Auth::id())->where('is_pay', 1)->count();
        }

        return compact('total', 'members', 'self');
    }

    public function list()
    {
        $time = request('time');
        $model = MemberOrder::where('member_order.is_pay', 1)
            ->select('m.phone', 'm.avatar', 'member_order.money', 'member_order.pay_time')
            ->leftJoin('members as m', 'm.id', '=', 'member_order.member_id');

        if ($time == 'yd') {
            $model->whereDate('member_order.pay_time', Carbon::yesterday());
        }elseif ($time == '7') {
            $model->whereDate('member_order.pay_time', '<', Carbon::today()->subDay(7));
        }elseif ($time == 'month') {
            $model->whereDate('member_order.pay_time', '<', Carbon::today()->subDay(30));
        }else{
            $model->whereDate('member_order.pay_time', '=', Carbon::today());
        }
        $list = $model->get();
        return $list;
    }
}